package org.glut.backend.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import lombok.extern.slf4j.Slf4j;
import org.glut.backend.domain.Marksheet;
import org.glut.backend.domain.Slideshow;
import org.glut.backend.service.impl.MarksheetServiceImpl;
import org.glut.backend.service.impl.SlideshowServiceImpl;
import org.glut.backend.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 *  前端控制器
 *
 * @author wubinbin
 * @since 2025-08-05 00:32
 */
@RestController
@Slf4j
public class MarksheetController {
    @Autowired
    private MarksheetServiceImpl marksheetService;
    @Autowired
    RedisUtil redisUtil;

    @PostMapping("/public/marksheet/query")
    public ResponseEntity<List<Object>> marksheetquery(@RequestBody Marksheet condition) {
        log.info("查询评分数据条件：{}", condition);

        //用列名构建查询条件
        QueryWrapper<Marksheet> queryWrapper = new QueryWrapper<>(Marksheet.class);
        queryWrapper.eq("scoreid", condition.getScoreid());


        String key ="marksheet:query"+condition.getScoreid();
        List<Object> marksheetList = null;
        if(!redisUtil.hasKey(key)){
            List _marksheetList = this.marksheetService.list(queryWrapper);
            if(!CollectionUtils.isEmpty(_marksheetList)){
                //缓存
                redisUtil.lSet(key,_marksheetList);
                return ResponseEntity.ok(_marksheetList);
            }
        }
        else{
            marksheetList=redisUtil.lGet(key,0,-1);
        }
        return ResponseEntity.ok(marksheetList);
    }
}
